﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace ViewCam
{
    public partial class SettingsForm : Form
    {
        public SettingsForm()
        {
            InitializeComponent();
        }

        #region Properties
        /// <summary>
        /// The LBL status
        /// </summary>
        public ToolStripLabel lblStatus;
        /// <summary>
        /// The menu settings
        /// </summary>
        public ToolStripMenuItem menuSettings;
        #endregion

        #region Declarations
        ViewCamHelper viewcamModule;
        FolderBrowserDialog folder;
        #endregion

        #region Methods
        /// <summary>
        /// Handles the Load event of the SettingsForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void SettingsForm_Load(object sender, EventArgs e)
        {
            this.Text = "Settings";
            ReadAll();
            SetTheme();
        }

        /// <summary>
        /// Handles the Click event of the btnSelect control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnSelect_Click(object sender, EventArgs e)
        {
            viewcamModule = new ViewCamHelper();

            folder = new FolderBrowserDialog();
            folder.ShowDialog();

            if (folder.SelectedPath != "")
            {
                this.txtRecordPath.Text = folder.SelectedPath;
            }
        }

        /// <summary>
        /// Handles the Click event of the btnShare control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnShare_Click(object sender, EventArgs e)
        {
            viewcamModule = new ViewCamHelper();
            viewcamModule.ShareFolder(this.txtRecordPath.Text, "View Cam Videos", "View Cam Shared Videos Folder");
        }

        /// <summary>
        /// Handles the Click event of the btnOK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            SaveAll();
            EnableMenu();
            this.Close();
        }

        /// <summary>
        /// Reads all.
        /// </summary>
        private void ReadAll()
        {
            try
            {
                viewcamModule = new ViewCamHelper();
                this.txtRecordPath.Text = viewcamModule.GetConfigSettings("RecordPath", lblStatus);
                this.lblBeepFreq.Text = viewcamModule.GetConfigSettings("Frequency", lblStatus);
                this.lblBeepDuration.Text = viewcamModule.GetConfigSettings("Duration", lblStatus);
                this.lblAutoStop.Text = viewcamModule.GetConfigSettings("AutoRecord", lblStatus);
                this.lblAutoNotify.Text = viewcamModule.GetConfigSettings("AutoNotify", lblStatus);
                this.tbTheme.Text = viewcamModule.GetConfigSettings("Theme", lblStatus);
                this.lblTray.Text = viewcamModule.GetConfigSettings("HideToTray", lblStatus);
                this.lblMaxed.Text = viewcamModule.GetConfigSettings("MaxedStart", lblStatus);

                this.trkbarBeepFreq.Value = Convert.ToInt32(this.lblBeepFreq.Text);
                this.trkbarBeepDuration.Value = Convert.ToInt32(this.lblBeepDuration.Text);
                this.trkbarAutoStop.Value = Convert.ToInt32(this.lblAutoStop.Text);
                this.trkbarAutoNotify.Value = Convert.ToInt32(this.lblAutoNotify.Text);
                this.chkbxTray.Checked = Convert.ToBoolean(this.lblTray.Text);
                this.chkbxMaxed.Checked = Convert.ToBoolean(this.lblMaxed.Text);

                if (this.tbTheme.Text != "")
                {
                    this.pbTheme.BackColor = viewcamModule.HexToColor(this.tbTheme.Text);
                }
            }
            catch (Exception)
            {   }
        }

        /// <summary>
        /// Saves all.
        /// </summary>
        private void SaveAll()
        {
            viewcamModule = new ViewCamHelper();
            viewcamModule.SaveConfigSettings("RecordPath", this.txtRecordPath);
            viewcamModule.SaveConfigSettings("Frequency", this.lblBeepFreq);
            viewcamModule.SaveConfigSettings("Duration", this.lblBeepDuration);
            viewcamModule.SaveConfigSettings("AutoRecord", this.lblAutoStop);
            viewcamModule.SaveConfigSettings("AutoNotify", this.lblAutoNotify);
            viewcamModule.SaveConfigSettings("HideToTray", this.lblTray);
            viewcamModule.SaveConfigSettings("MaxedStart", this.lblMaxed);

            if (this.tbTheme.Text != viewcamModule.GetConfigSettings("Theme", lblStatus))
            {
                viewcamModule.SaveConfigSettings("Theme", this.tbTheme);
                MessageBox.Show("Note: For Theme Color to effect on Main Window, Please restart the application. ", "View Cam");
            }
            else
            {
                viewcamModule.SaveConfigSettings("Theme", this.tbTheme);
            }

            viewcamModule.WriteConfigSettings();
        }

        /// <summary>
        /// Handles the Scroll event of the trkbarBeepFreq control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void trkbarBeepFreq_Scroll(object sender, EventArgs e)
        {
            this.lblBeepFreq.Text = trkbarBeepFreq.Value.ToString();
        }

        /// <summary>
        /// Handles the Scroll event of the trkbarBeepDuration control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void trkbarBeepDuration_Scroll(object sender, EventArgs e)
        {
            this.lblBeepDuration.Text = trkbarBeepDuration.Value.ToString();
        }

        /// <summary>
        /// Handles the Scroll event of the trkbarAutoStop control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void trkbarAutoStop_Scroll(object sender, EventArgs e)
        {
            this.lblAutoStop.Text = trkbarAutoStop.Value.ToString();
        }

        /// <summary>
        /// Handles the Scroll event of the trkbarAutoNotify control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void trkbarAutoNotify_Scroll(object sender, EventArgs e)
        {
            this.lblAutoNotify.Text = trkbarAutoNotify.Value.ToString();
        }

        /// <summary>
        /// Handles the Click event of the btnTest control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnTest_Click(object sender, EventArgs e)
        {
            viewcamModule = new ViewCamHelper();
            viewcamModule.TestBeepSound(Convert.ToInt32(this.lblBeepFreq.Text), Convert.ToInt32(this.lblBeepDuration.Text));
        }

        /// <summary>
        /// Handles the CheckedChanged event of the chkbxTray control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
 
        private void chkbxTray_CheckedChanged(object sender, EventArgs e)
        {
            if (chkbxTray.Checked == true)
            {
                lblTray.Text = "true";
            }
            else
            {
                lblTray.Text = "false";
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the chkbxMaxed control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void chkbxMaxed_CheckedChanged(object sender, EventArgs e)
        {
            if (chkbxMaxed.Checked == true)
            {
                lblMaxed.Text = "true";
            }
            else
            {
                lblMaxed.Text = "false";
            }
        }

        /// <summary>
        /// Enables the menu.
        /// </summary>
        private void EnableMenu()
        {
            menuSettings.Enabled = true;
        }

        /// <summary>
        /// Handles the Click event of the btnAddIPCam control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnAddIPCam_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// CPBs the theme_ picked color change.
        /// </summary>
        /// <param name="PickedColor">Color of the picked.</param>
        private void cpbTheme_PickedColorChange(Color PickedColor)
        {
            Color color = this.cpbTheme.PickedColor;

            this.pbTheme.BackColor = this.cpbTheme.PickedColor;
            this.tbTheme.Text = this.cpbTheme.PickedColor.Name;

        }

        /// <summary>
        /// Sets the theme.
        /// </summary>
        private void SetTheme()
        {
            viewcamModule = new ViewCamHelper();
            this.BackColor = viewcamModule.SetThemeColor(lblStatus);
        }
        #endregion
    }
}
